package com.ruoyi.web.controller.system;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.system.domain.PracticeBase;
import com.ruoyi.system.domain.PracticeBaseUser;
import com.ruoyi.system.domain.TrainingBase;
import com.ruoyi.system.domain.TrainingBaseUser;
import com.ruoyi.system.domain.TrainingCadet;
import com.ruoyi.system.service.IPracticeBaseService;
import com.ruoyi.system.service.IPracticeBaseUserService;
import com.ruoyi.system.service.ISysMenuService;
import com.ruoyi.system.service.ITrainingBaseService;
import com.ruoyi.system.service.ITrainingBaseUserService;
import com.ruoyi.system.service.ITrainingCadetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@Api(tags = "登录验证")
@RestController
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;
    @Autowired
    private ISysMenuService menuService;
    @Autowired
    private SysPermissionService permissionService;
    @Autowired
    private ITrainingBaseService trainingBaseService;
    @Autowired
    private IPracticeBaseService practiceBaseService;
    @Autowired
    private ITrainingCadetService trainingCadetService;
    @Autowired
    ITrainingBaseUserService trainingBaseUserService;
    @Autowired
    IPracticeBaseUserService practiceBaseUserService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @ApiOperation("登录方法")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @ApiOperation("获取用户信息")
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        AjaxResult ajax = AjaxResult.success();

        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        // 基地集合
        user.getRoles().forEach(role -> {
            //培训
            if ("baseAdmin".equals(role.getRoleKey())) {
                TrainingBaseUser trainingBaseUser = new TrainingBaseUser();
                trainingBaseUser.setUserId(user.getUserId());
                List<TrainingBaseUser> trainingBaseUserList = trainingBaseUserService.selectTrainingBaseUserList(trainingBaseUser);
                TrainingBase trainingBase = new TrainingBase();
                if (trainingBaseUserList.size() > 0) {
                    trainingBase.setBaseId(trainingBaseUserList.get(0).getBaseId());
                    List<TrainingBase> trainingBaseList = trainingBaseService.selectTrainingBaseList(trainingBase);
                    ajax.put("trainingBase", trainingBaseList);
                }
            }
            //实践
            else if ("basicAdmin".equals(role.getRoleKey())) {
                PracticeBaseUser practiceBaseUser = new PracticeBaseUser();
                practiceBaseUser.setUserId(user.getUserId());
                List<PracticeBaseUser> practiceBaseUserList = practiceBaseUserService.selectPracticeBaseUserList(practiceBaseUser);
                PracticeBase practiceBase = new PracticeBase();
                if (practiceBaseUserList.size() > 0) {
                    practiceBase.setBaseId(practiceBaseUserList.get(0).getBaseId());
                    List<PracticeBase> practiceBaseList = practiceBaseService.selectPracticeBaseList(practiceBase);
                    ajax.put("practiceBase", practiceBaseList);
                }
            }
            //学员信息
            else if ("student".equals(role.getRoleKey())) {
                TrainingCadet trainingCadet = new TrainingCadet();
                trainingCadet.setCadetCode(user.getUserName());
                List<TrainingCadet> trainingCadets = trainingCadetService.selectTrainingCadetList(trainingCadet);
                ajax.put("trainingCadet", trainingCadets);
            }
        });

        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @ApiOperation("获取路由信息")
    @GetMapping("getRouters")
    public AjaxResult getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
